package com.example.base_kotlin_xml.view

import androidx.navigation.NavOptions
import androidx.navigation.fragment.findNavController
import com.blankj.utilcode.util.LogUtils
import com.example.base_kotlin_xml.R
import com.example.base_kotlin_xml.databinding.FragmentLoginBinding
import com.example.base_model.view.BaseFragment
import dagger.hilt.android.AndroidEntryPoint

/**
 * @Author: jiangKunKun
 * @CreateDate: 2025/11/21
 * @Description: 登录
 * @SpittingGrooves:
 */
@AndroidEntryPoint
class LoginFragment : BaseFragment<FragmentLoginBinding>() {
    /**
     * 是否为强制登录模式
     */
    private var isForceLogin: Boolean = false

    override fun initObservers() {
        // 初始化时获取参数
        arguments?.let { bundle ->
            isForceLogin = bundle.getBoolean("isForceLogin", false)
        }
    }

    override fun initClickEvents() {
        // 去登录
        mBinding.btnGoLogin.setOnClickListener {
            try {
                val navController = findNavController()
                if (isForceLogin) {
                    // 强制登录：跳转到主页
                    // 使用popUpTo清理到主页并导航
                    val navOptions = NavOptions.Builder()
                        .setPopUpTo(R.id.homeFragment, inclusive = true)
                        .build()
                    navController.navigate(R.id.homeFragment, null, navOptions)
                } else {
                    // 非强制登录：返回到原来的页面
                    if (!navController.popBackStack()) {
                        // 如果无法返回，则导航到主页
                        navController.navigate(R.id.homeFragment)
                    }
                }
            } catch (e: Exception) {
                LogUtils.e("登录成功后导航失败", e.message)
            }
        }
    }

    override fun initData() {

    }

    override fun attachLayoutRes(): Int = R.layout.fragment_login

}